/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phongmodel;

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;

/**
 *
 * @author Grzesiek
 */
public class Renderer {

    //public static boolean LIGHT_COLOR = false;

    public static void paintImg(JPanel panel, MySphere sphere, Light light, MyCamera cam, Graphics graphics,boolean LIGHT_COLOR) {
        double xx, yy, z;
        Point3D actualPoint;
        Color resultColor;
        //Graphics graphics = panel.getGraphics();
        double rr = Math.pow((sphere.getRayon()), 2);
        double xSize = panel.getBounds().width;
        double ySize = panel.getBounds().height;

        graphics.translate((int) (xSize / 2), (int) (ySize / 2));


        //iteration across all pixels
        for (int x = (int) (-xSize / 2); x < xSize / 2; x++) {
            for (int y = (int) (-xSize / 2); y < ySize / 2; y++) {

                xx = Math.pow((x - sphere.getCenter().getX()), 2);
                yy = Math.pow((y - sphere.getCenter().getX()), 2);

                //see if pixel is in sphere
                if ((xx + yy) < rr) {
                    //compute z from x^2 + y^2 + z^2 = r^2
                    z = Math.sqrt(rr - yy - xx);
                    actualPoint = new Point3D(x, y, z);

                    //System.out.println(x + " " + y + " " + z);

                    Vector3D L = new Vector3D(light.getLocation(), actualPoint);
                    Vector3D N = new Vector3D(actualPoint, sphere.getCenter());
                    Vector3D V = new Vector3D(cam.getPunkt(), actualPoint);

                    L.normalize();
                    N.normalize();
                    V.normalize();

                    // R = 2*N*(NdotL)-L
                    double NdotL = Vector3D.dot(N, L);
                    Vector3D tmp = N.copy();
                    tmp.scalarMulti(2 * NdotL);
                    Vector3D R = Vector3D.sub(tmp, L);
                    R.normalize();

                    if (LIGHT_COLOR) {
                        
                        resultColor = MyPhong.calculateColor(sphere, light, N, L, R, V, 1.0);
                              
                    } else {
                        double result = MyPhong.PhongCalculate(sphere, light, N, L, R, V, 1.0);
                        result = norm0to1(result);

                        //calculate color
                        double r = sphere.getKolor().getRed();
                        double g = sphere.getKolor().getGreen();
                        double b = sphere.getKolor().getBlue();

                        r *= result;
                        g *= result;
                        b *= result;

                        resultColor = new Color((int) r, (int) g, (int) b);
                    }

                    drawPixel(x, y, resultColor, graphics);
                }

            }
        }
    }

    public static void drawPixel(int x, int y, Color c, Graphics g) {
        g.setColor(c);
        g.drawRect(x, y, 1, 1);
    }

    //check if number from <0;1>
    public static double norm0to1(double d) {
        if (d <= 0.0) {
            return 0.0;
        }
        if (d >= 1.0) {
            return 1.0;
        }
        return d;
    }
}
